package org.example.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

/**
 * @author wwy
 * @date 2020-11-28 15:26
 */
public class CmdUtils {

    public String waitFor(Process p) {
        InputStream in = null;
        BufferedReader br = null;
        InputStream error = null;
        String result = "error";
        int exitValue = -1;
        StringBuffer outputString = new StringBuffer();
        try {
            in = p.getInputStream();
            br = new BufferedReader(new InputStreamReader(in, Charset.forName("GBK")));
            error = p.getErrorStream();
            boolean finished = false;
            //每次休眠1秒，最长执行时间10分种
            int maxRetry = 600;
            int retry = 0;
            String line = null;
            while ((line = br.readLine()) != null) {
                outputString.append(line+"\n");
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    System.out.println(e.getMessage());
                }
            }
        }
        return outputString.toString();

    }
}
